package org.maxkey.portal.api.controller;
import lombok.extern.slf4j.Slf4j;
import org.maxkey.portal.core.authentication.CurrentAccount;
import org.maxkey.portal.core.cam.CurrentThreadLocal;
import org.maxkey.portal.core.cam.PageResultData;
import org.maxkey.portal.core.cam.ResultData;
import org.maxkey.portal.core.search.ParamModel;
import org.maxkey.portal.core.search.model.DocParam;
import org.maxkey.portal.model.Doc;
import org.maxkey.portal.service.DocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/doc")
@Slf4j
public class DocController extends BaseController {

    @Autowired
    private DocService docService;

    @PostMapping(value = "/findAll",produces="application/json;charset=utf-8")
    public ResultData findAll() {
        return docService.findAll();
    }

    @PostMapping(value = "/findList",produces="application/json;charset=utf-8")
    public PageResultData findList(@RequestBody ParamModel<DocParam> searData) {

        return docService.findList(searData);
    }

    @PostMapping(value = "/find/{id}")
    public ResultData find(@PathVariable Integer id) { return docService.findById(id); }

    @PostMapping(value = "/info")
    public ResultData infi() {
        CurrentAccount user= CurrentThreadLocal.get();
        return docService.findById(user.getId());
    }

    @PostMapping(value = "/remove/{id}")
    public ResultData remove(@PathVariable Long id) {
        return docService.deleteById(id);
    }

    @PostMapping(value = "/save",produces="application/json;charset=utf-8")
    public ResultData save(@RequestBody Doc doc) {
        CurrentAccount user= CurrentThreadLocal.get();
        //设置文档发布人
        doc.setUserid(user.getId());
        doc.setUsername(user.getName());
        return docService.save(doc);
    }

}
